package mylab.utils.common;

import java.time.format.DateTimeFormatter;
import java.util.regex.Pattern;

import cn.hutool.core.date.DatePattern;
import lombok.experimental.UtilityClass;

@UtilityClass
public class DateTimeFormatterUtil {
    // 常见日期格式的正则表达式
    final Pattern DATE_PATTERN = Pattern.compile(
            "^(\\d{4}-\\d{2}-\\d{2})|" +              // yyyy-MM-dd
                    "(\\d{4}\\d{2}\\d{2})|" +         // yyyyMMdd
                    "(\\d{4}/\\d{2}/\\d{2})|" +       // yyyy/MM/dd
                    "(\\d{2}/\\d{2}/\\d{4})|" +       // MM/dd/yyyy
                    "(\\d{2}-\\d{2}-\\d{4})$"         // MM-dd-yyyy
    );

    // 常见时间格式的正则表达式
    final Pattern TIME_PATTERN = Pattern.compile(
            "^(\\d{2}:\\d{2}:\\d{2})|" +      // HH:mm:ss
                    "(\\d{2}:\\d{2})$"                // HH:mm
    );

    final Pattern DATETIME_PATTERN = Pattern.compile(
            "^(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2})|" +                // yyyy-MM-dd HH:mm:ss
                    "(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3})|" +         // yyyy-MM-dd HH:mm:ss.SSS
                    "(\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2})|" +                // MM/dd/yyyy HH:mm:ss
                    "(\\d{2}/\\d{2}/\\d{4} \\d{2}:\\d{2}:\\d{2}.\\d{3})|" +         // MM/dd/yyyy HH:mm:ss.SSS
                    "(\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2})|" +                // yyyy/MM/dd HH:mm:ss
                    "(\\d{4}/\\d{2}/\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3})|" +         // yyyy/MM/dd HH:mm:ss.SSS
                    "(\\d{2}-\\d{2}-\\d{4} \\d{2}:\\d{2}:\\d{2})|" +                // dd-MM-yyyy HH:mm:ss
                    "(\\d{2}-\\d{2}-\\d{4} \\d{2}:\\d{2}:\\d{2}.\\d{3})$"           // dd-MM-yyyy HH:mm:ss.SSS
    );

    public boolean isDateTime(String input) {
        return isDateTime(input) || isTimeByReg(input) || isDateTimeByReg(input);
    }

    public boolean isDateByReg(String input) {
        return DATE_PATTERN.matcher(input).matches();
    }

    public boolean isTimeByReg(String input) {
        return TIME_PATTERN.matcher(input).matches();
    }

    public boolean isDateTimeByReg(String input) {
        return DATETIME_PATTERN.matcher(input).matches();
    }


    public DateTimeFormatter normalDate() {
        return DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN);
    }

    public DateTimeFormatter normalTime() {
        return DateTimeFormatter.ofPattern(DatePattern.NORM_TIME_PATTERN);
    }

    public DateTimeFormatter normalDateTime() {
        return DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
    }

    public DateTimeFormatter normalDateTimeMills() {
        return DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_MS_PATTERN);
    }
}
